//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jcx.ldzj.utile;

import okhttp3.*;
import okhttp3.Request.Builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * okhttp工具类
 */
public class OkHttpUtil {
    private static final Logger log = LoggerFactory.getLogger(OkHttpUtil.class);
    public static MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static MediaType XML = MediaType.parse("application/xml; charset=utf-8");
    public static MediaType TYPE = MediaType.parse("Content-type;charset=utf-8");


    public OkHttpUtil() {
    }
    /**
     * 不传请求头的get请求
     * @param url
     * @param queries
     * @return
     */
    public static String get(String url, Map<String, String> queries) {
        Map hadler=new HashMap();
        hadler.put("Content-type","charset=utf-8");
        return get(url, hadler, null);
    }
    /**
     * 不传请求头的get请求
     * @param url
     * @return
     */
    public static String get(String url) {
        Map hadler=new HashMap();
        hadler.put("Content-type","charset=utf-8");
        return get(url,hadler, null);
    }
    public static String OkGetArt(String url){
        String html = null;
        OkHttpClient client = new OkHttpClient();
        Request requestObj = new Request.Builder().url(url).addHeader("Content-type","charset=utf-8").build();
        try (Response responseObj = client.newCall(requestObj).execute()){
            html = responseObj.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
    /**
     * 加上请求头的get请求
     * @param url
     * @param header
     * @param queries
     * @return
     */
    public static String get(String url, Map<String, String> header, Map<String, String> queries) {
        StringBuffer sb = new StringBuffer(url);
        if (queries != null && queries.keySet().size() > 0) {
            sb.append("?");
            queries.forEach((k, v) -> {
                sb.append("&").append(k).append("=").append(v);
            });
        }

        Builder builder = new Builder();
        if (header != null && header.keySet().size() > 0) {
            header.forEach(builder::addHeader);
        }

        Request request = builder.url(sb.toString()).build();
        return getBody(request);
    }

    /**
     * 不加请求头的post请求
     * @param url
     * @param params
     * @return
     */
    public static String post(String url, Map<String, String> params) {
        return post(url, (Map)null, params);
    }

    /**
     * 加请求头的post请求
     * @param url
     * @param header
     * @param params
     * @return
     */
    public static String post(String url, Map<String, String> header, Map<String, String> params) {
        okhttp3.FormBody.Builder formBuilder = (new okhttp3.FormBody.Builder()).add("clientId", "blade");
        if (params != null && params.keySet().size() > 0) {
            params.forEach(formBuilder::add);
        }

        Builder builder = new Builder();
        if (header != null && header.keySet().size() > 0) {
            header.forEach(builder::addHeader);
        }

        Request request = builder.url(url).post(formBuilder.build()).build();
        return getBody(request);
    }

    /**
     * 传json的不加请求头的post请求
     * @param url
     * @param json
     * @return
     */
    public static String postJson(String url, String json) {
        return postJson(url, (Map)null, json);
    }

    /**
     * 传json的加请求头的post请求
     * @param url
     * @param header
     * @param json
     * @return
     */
    public static String postJson(String url, Map<String, String> header, String json) {
        return postContent(url, header, json, JSON);
    }

    /**
     * 传xml的不加请求头的post请求
     * @param url
     * @param xml
     * @return
     */
    public static String postXml(String url, String xml) {
        return postXml(url, (Map)null, xml);
    }

    /**
     * 传xml的加请求头的post请求
     * @param url
     * @param header
     * @param xml
     * @return
     */
    public static String postXml(String url, Map<String, String> header, String xml) {
        return postContent(url, header, xml, XML);
    }

    /**
     * 发起请求
     * @param url
     * @param header
     * @param content
     * @param mediaType
     * @return
     */
    public static String postContent(String url, Map<String, String> header, String content, MediaType mediaType) {
        RequestBody requestBody = RequestBody.create(mediaType, content);
        Builder builder = new Builder();
        if (header != null && header.keySet().size() > 0) {
            header.forEach(builder::addHeader);
        }

        Request request = builder.url(url).post(requestBody).build();
        return getBody(request);
    }

    /**
     * 获取返回值
     * @param request
     * @return
     */
    private static String getBody(Request request) {
        String responseBody = "";
        Response response = null;

        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                String var4 = response.body().string();
                return var4;
            }
        } catch (Exception var8) {
            log.error("okhttp3 post error >> ex = {}", var8.getMessage());
        } finally {
            if (response != null) {
                response.close();
            }

        }

        return responseBody;
    }
}
